package com.qiaoyanqing.textdemoqyq7.Activities;

import android.content.Intent;
import android.content.IntentFilter;
import android.graphics.Color;
import android.net.ConnectivityManager;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;

import com.qiaoyanqing.textdemoqyq7.Adapters.MainAdapter;
import com.qiaoyanqing.textdemoqyq7.Broadcases.IntReceiver;
import com.qiaoyanqing.textdemoqyq7.Fragments.BaikeFragment;
import com.qiaoyanqing.textdemoqyq7.Fragments.JingyingFragment;
import com.qiaoyanqing.textdemoqyq7.Fragments.ShujuFragment;
import com.qiaoyanqing.textdemoqyq7.Fragments.ToutiaoFragment;
import com.qiaoyanqing.textdemoqyq7.Fragments.ZixunFragment;
import com.qiaoyanqing.textdemoqyq7.R;

import java.util.ArrayList;
import java.util.List;

/**
 * 总结：大致功能都实现了，缺陷是，没有判断网络状态，也就说，打开app的
 * 时候判断，没有网的情况下显示数据库数据，或者给一张图。
 */




public class MainActivity extends AppCompatActivity {
    private TabLayout mTabLayout;
    private ViewPager mViewPager;
    private List<Fragment> mData;
    private IntReceiver mReceiver;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //网络
        IntentFilter filter=new IntentFilter();
        filter.addAction(ConnectivityManager.CONNECTIVITY_ACTION);
        registerReceiver(mReceiver,filter);


        initView();
        initData();
        initViewpager();
        initTablayout();

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        unregisterReceiver(mReceiver);
    }

    private void initTablayout() {
        String titles[] = getResources().getStringArray(R.array.title);
        for (int i = 0; i < titles.length; i++) {
            TabLayout.Tab tab = mTabLayout.newTab();
            tab.setText(titles[i]);
            mTabLayout.setTabGravity(TabLayout.GRAVITY_FILL);
            mTabLayout.addTab(tab);
        }

        mTabLayout.setTabMode(TabLayout.MODE_FIXED);
        mTabLayout.addOnTabSelectedListener(new TabLayout.ViewPagerOnTabSelectedListener(mViewPager));
        // 指示线的颜色.字体选中后的颜色
        mTabLayout.setSelectedTabIndicatorColor(Color.GREEN);
        //参数1是未选中的颜色，参数2是选中的颜色。
        mTabLayout.setTabTextColors(Color.BLACK, Color.GREEN);


    }

    private void initViewpager() {
        MainAdapter mainAdapter = new MainAdapter(getSupportFragmentManager(), mData);
        mViewPager.setAdapter(mainAdapter);
        mViewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(mTabLayout));
    }

    private void initData() {
        mData = new ArrayList<>();
        BaikeFragment baikeFragment = new BaikeFragment();
        JingyingFragment jingyingFragment = new JingyingFragment();
        ShujuFragment shujuFragment = new ShujuFragment();
        ToutiaoFragment toutiaoFragment = new ToutiaoFragment();
        ZixunFragment zixunFragment = new ZixunFragment();

        mData.add(toutiaoFragment);
        mData.add(baikeFragment);
        mData.add(zixunFragment);
        mData.add(jingyingFragment);
        mData.add(shujuFragment);


    }

    private void initView() {
        mTabLayout = (TabLayout) findViewById(R.id.tablayout);
        mViewPager = (ViewPager) findViewById(R.id.viewpager);

    }

    public void imageclick(View view) {
        Intent intent = new Intent(this, CollectionActivity.class);
        startActivity(intent);

    }
}
